# pita: An invisible audio player
# Copyright (C) 2006-2007 Austin Bingham
#
# This program is free software# you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation# either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# You can reach the author at:
#     austin.bingham@gmail.com

import cPickle, sys
from SimpleXMLRPCServer import *

class Server(SimpleXMLRPCServer):
    def __init__(self, address, player):
        self.allow_reuse_address = True
        
        SimpleXMLRPCServer.__init__(self,
                                    address,
                                    logRequests=False)
                                    
        self.player = player
        self.terminate = False

        self.register_function(self.append,
                               'append')
        self.register_function(self.clear,
                               'clear')
        self.register_function(self.remove,
                               'remove')
        self.register_function(self.next,
                               'next')
        self.register_function(self.previous,
                               'previous')
        self.register_function(self.jump_to,
                               'jump_to')
        self.register_function(self.pause,
                               'pause')
        self.register_function(self.unpause,
                               'unpause')
        self.register_function(lambda: self.player.is_paused(),
                               'is_paused')
        self.register_function(self.pause_play,
                               'pause_play')
        self.register_function(lambda: self.player.random,
                               'is_random')
        self.register_function(self.set_random,
                               'set_random')
        self.register_function(lambda: self.player.looping,
                               'is_looping')
        self.register_function(self.set_looping,
                               'set_looping')
        self.register_function(lambda: self.player.sample_rate,
                               'sample_rate')
        self.register_function(self.set_sample_rate,
                               'set_sample_rate')
        self.register_function(self.kill,
                               'kill')
        self.register_function(self.ping,
                               'ping'),
        self.register_function(self.get_playlist,
                               'get_playlist')

    def serve(self):
        while not self.terminate:
            self.handle_request()
        self.server_close()
        
    def append(self, filenames):
        self.player.append([str(f.data) for f in filenames])
        return True

    def clear(self):
        self.player.clear()
        return True

    def remove(self, pos):
        self.player.remove(pos)
        return True

    def next(self):
        self.player.next()
        return True

    def previous(self):
        self.player.previous()
        return True

    def jump_to(self, pos):
        self.player.jump_to(pos)
        return True

    def pause(self):
        self.player.paused = True
        return True

    def unpause(self):
        self.player.paused = False
        return True

    def set_random(self, r):
        self.player.random = r
        return True

    def set_looping(self, l):
        self.player.looping = l
        return True

    def set_sample_rate(self, r):
        self.player.sample_rate = r
        return True

    def pause_play(self):
        if self.player.paused: self.player.paused = False
        else: self.player.paused = True
        return True

    def kill(self):
        self.terminate = True
        return True

    def ping(self):
        return True

    def get_playlist(self):
        return cPickle.dumps(self.player.get_playlist())
